/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.jcbp.wizards.jcapplet.pages;

import java.io.IOException;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipsejcde.core.JCDECore;
import org.eclipsejcde.jcbp.JCBPCore;
import org.eclipsejcde.jcbp.wizards.Activator;
import org.eclipsejcde.wizards.jcapplet.pages.NewJCAppletWizardPage;

/**
 * @author Amgad S Youssef
 *
 */
public class NewJCBPAppletWizardPage extends NewJCAppletWizardPage {
	@Override
	public void init(IStructuredSelection selection) {
		super.init(selection);
		setSuperClass("sosframework.SOSApplet", false);
		setMethodStubSelection(false, false, true, false);
	}

	public NewJCBPAppletWizardPage() {
		setTitle("SOS Java Card Applet");
		setDescription("Create a new Java Card Applet class that uses a Secure Object Sharing framework.");
	}

	@Override
	public void createType(IProgressMonitor monitor) throws CoreException, InterruptedException {
		try {
			// enable JCBP project nature
			JCBPCore.enableJCBPProjectNature(this.getPackageFragmentRoot().getJavaProject());

			// extracting sosframework.jar contents
			JCBPCore.extractSOSFrameworkJar(this.getPackageFragmentRoot().getResource().getLocation().toFile().toString());

			// refresh workspace
			ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, monitor);

			// create applet
			super.createType(monitor);

			// update constructor
			IType createdType = getCreatedType();

			StringBuffer content = new StringBuffer();
			IMethod constructor = createdType.getMethod(createdType.getElementName(), new String[] {});
			if (constructor != null && constructor.exists()) {
				content = new StringBuffer(constructor.getSource());
				content.insert(content.indexOf("{") + 1, "\n\tsuper((byte) 0, (byte) 0, (byte) 10, CLEAR_ON_RESET);");
				constructor.delete(true, monitor);
			} else {
				content.append("public " + createdType.getElementName() + "() {\n");
				content.append("\tsuper((byte) 0, (byte) 0, (byte) 10, CLEAR_ON_RESET);\n");
				content.append("}");
			}

			// add constructor before all methods
			IMethod[] methods = createdType.getMethods();
			IJavaElement sibling = null;
			sibling = null;
			if (methods.length > 0) {
				sibling = methods[0];
			}
			IStatus status = JCDECore.formatMethodCode(content.toString(), createdType.getJavaProject());
			if (status.getSeverity() == IStatus.OK) {
				createdType.createMethod(status.getMessage(), sibling, true, monitor);
			} else {
				createdType.createMethod(content.toString(), sibling, true, monitor);
				if (status.getSeverity() == IStatus.ERROR) {
					throw new CoreException(status);
				}
			}
		} catch (IOException e) {
			throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.OK, e.getMessage(), e));
		}
	}

}
